package com.enjava;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.springframework.beans.factory.annotation.Autowired;

import libreria.Editorial;
import libreria.Libro;

@FacesConverter("editorialConverter")
public class EditorialConverter implements Converter{
	


	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		Long idEditorial = Long.parseLong(value);
		EntityManagerFactory em = getEMF();
		return em.createEntityManager().find(Editorial.class, idEditorial);
	}

	private EntityManagerFactory getEMF() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		EntityManagerFactory em=(EntityManagerFactory) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "entityManagerFactory");
		return em;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {

		return ((Editorial)value).getId()+"";
	}

	
}
